package com.fz.controller;

import com.fz.mapper.BookMapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by webrx on 2017-09-04.
 */
@Controller
@RequestMapping("/user")
public class Hello{
    ///user/show
    @RequestMapping("/show")
    public void show(){
        System.out.println("hello"); // views/user/show.jsp 视图渲染显示
    }

//    @RequestMapping("")  //http://localhost/user/
//    public void aa(HttpServletResponse resp) throws IOException {
//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset-utf-8");
//        PrintWriter out = resp.getWriter();
//        out.print("Hello - aa");
//        out.flush();
//        out.close();
//    }

    @RequestMapping(value = "bb.action")
    public void bb(PrintWriter out){
        out.print("hello-bb要要工");
    }

    @RequestMapping(value = {"c1","c2","c3.action"})
    public String cc(){
        return "cc"; //views/cc.jsp 视图
    }

    @RequestMapping("dd") @ResponseBody
    public String dd(){
        System.out.println("hello");
        return null;  //默认找 views/user/dd.jsp
    }

    @RequestMapping(value="/ee",method = RequestMethod.POST)
    public void ee(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset-utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<h3>Hello World  中文效果</h3>");
        out.print(req.getParameter("name"));
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();

    }

    @RequestMapping("/book")
    public String book(Model model){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        model.addAttribute("books",bdao.queryAll());
        return "book";
    }

    @RequestMapping("/del/{id}")
    //@ResponseBody
    public String del(@PathVariable("id") int id){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        bdao.deleteById(id);
        ss.commit();
        //return null;
        return "redirect:/book";
    }



    @RequestMapping("/ff")
    public String ff(Model m){
        m.addAttribute("name","李四四");
        return "ff";
    }

    @RequestMapping("/gg")
    public ModelAndView gg(){
        ModelAndView mv = new ModelAndView("aa"); //views/aa.jsp
        mv.addObject("name","jack8888888");
        mv.setViewName("user/save"); //views/user/save.jsp
        mv.setViewName("ff");//views/ff.jsp
        return mv;
    }

    @RequestMapping("/hh")
    public void hh(String name,PrintWriter out){
        out.print(name);
    }

    @RequestMapping(value="/save",method=RequestMethod.POST) @ResponseBody
    public void save(String name,int age,String address){
        System.out.printf("姓名：%s，年龄：%d岁，家庭地址：%s",name,age,address);
    }

    @RequestMapping(value="/add/{id}",method = RequestMethod.GET)
    public String add(@PathVariable("id") int sid,Model model){
        model.addAttribute("student_id",sid);
        return "add";
    }

    @RequestMapping(value="/addd/{name}/{c}",method = RequestMethod.GET) @ResponseBody
    public void addd(@PathVariable("c") int sid,@PathVariable("name") String sname,PrintWriter out){
        out.print(sid);
        out.print(sname);
    }

}
